// lib/Generator.js
const inquirer = require("inquirer");
const ora = require("ora");
const chalk = require("chalk");
const spinner = ora();

const downloadGit = require("./download-git");

class Generator {
  constructor(name, targetDir) {
    // 目录名称
    this.name = name;
    // 创建位置
    this.targetDir = targetDir;
    //当前的目录
    this.currentDir = process.cwd();
    this.projectType = {
      vite: "https://gitee.com/renmingxin/vite-react-group.git",
      umi: "https://gitee.com/renmingxin/umi-react.git",
    };
  }
  async userChoose() {
    const questionsList = [
      {
        name: "project_type",
        type: "list",
        message: "请选择您需要安装的项目类型: ",
        choices: [
          {
            name: "鑫仔推荐脚手架",
            value: "xinzai",
          },
          {
            name: "默认模版",
            value: "default",
          },
        ],
      },

      {
        /** 在选择了阿拉丁项目之后才有的选择 */
        when: (answer) => {
          return answer.project_type === "xinzai";
          // return /辣椒炒肉/.test(answer.goods.toString());
        },
        name: "theme_type",
        type: "list",
        message: "请选择您要使用的模板?",
        choices: [
          { name: "vite脚手架", value: "vite" },
          { name: "umi脚手架", value: "umi" },

          // {
          //   name: "管理",
          //   value: "manage",
          // },
          // {
          //   name: "安全",
          //   value: "safety",
          // },
          // {
          //   name: "服务",
          //   value: "serve",
          // },
          // {
          //   name: "运行",
          //   value: "run",
          // },
          // {
          //   name: "能源",
          //   value: "energy",
          // },
        ],
      },
    ];
    const questions = await inquirer.prompt(questionsList);
    return questions;
  }
  async downloadGit(repo, tag) {}
  getRemoteRepository(userChooseMsg) {
    const { project_type, theme_type } = userChooseMsg;
    let remoteRepository = "";
    if (project_type === "xinzai") {
      remoteRepository = this.projectType[theme_type];
    } else {
      remoteRepository = this.projectType.vite;
    }
    return {
      url: `direct:${remoteRepository}`,
      cliType: theme_type || 'vite',
    };
  }
  // 核心创建逻辑
  async create() {
    const userChooseMsg = await this.userChoose();
    const remoteRepositoryMsg = this.getRemoteRepository(userChooseMsg);

    //检查版本信息
    // spinner.start("检查新版本");
    // await checkVersion();
    // spinner.succeed("当前版本为1.0.0");

    //新建项目文件夹
    console.log();
    spinner.start(
      `✨  正在创建项目： ${chalk.yellow(this.name)}  模板为：${
        remoteRepositoryMsg.cliType
      }.`
    );
    const cloneGitErr = await downloadGit(
      remoteRepositoryMsg.url,
      this.targetDir
    );
    if (cloneGitErr) {
      spinner.fail();
      console.log(err);
    } else {
      console.log("成功");
      spinner.succeed();
    }
    console.log();
    spinner.succeed(`🎉  成功创建项目： ${chalk.yellow(this.name)}.`);
    console.log(
      `👉  Get started with the following commands:\n\n` +
        chalk.cyan(` ${chalk.gray("$")} cd ${this.name}\n`) +
        chalk.cyan(` ${chalk.gray("$")} npm install or yarn\n`) +
        chalk.cyan(` ${chalk.gray("$")} npm run dev or yarn run dev`)
    );
    console.log();
    console.log(chalk.cyan("开始卷起来吧！"));
  }
}

module.exports = Generator;
